package com.magus.project.yddemo.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.annotation.CustomPagingParam;
import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.core.vobean.PagingParam;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.framework.system.entity.Company;
import com.magus.framework.system.service.CompanyService;
import com.magus.project.yddemo.entity.IndicatorMapperDataLog;
import com.magus.project.yddemo.service.IndicatorMapperDataApiService;
import com.magus.project.yddemo.service.IndicatorMapperDataLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 指标管理
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Api(tags = "IndicatorDataExtractionLogManager", description = "指标数据抽取相关管理")
@RestController
@RequestMapping("/yddemo/indicatorDataExtractionLogs")
@AppGroupInfo(key = "yddemo:indicatorDataExtractionLog", name = "指标数据抽取相关管理", appKey = "yddemo")
public class IndicatorMapperDataLogController extends BaseController {

	@Autowired
	private IndicatorMapperDataLogService logService;
	
	@Autowired
	private IndicatorMapperDataApiService apiService;
	
	@Autowired
	private CompanyService companyService;

	/**
	 * 根据ID查询指标信息
	 * 
	 * @param indicatorId
	 *            指标ID
	 * @return 指标信息
	 * @throws ParseException 
	 */
	@GetMapping
	@RequiresPermissions("yddemo:indicatorDataExtractionLog:manage")
	@ApiOperation(value = "更新数据", notes = "更新数据信息")
	ResponseEntity<Void> findAll(@RequestParam(required = false) String companyId,@RequestParam(required = true) String dateTime) throws ParseException {
		if(!companyId.equals("")) {
			List<SearchFilter> searchFilters = new ArrayList<>();
			searchFilters.add(SearchFilter.equal("id", companyId));
			Specification<Company> spec = buildFilterRequest(searchFilters);
			Company company = companyService.findOne(spec);
			apiService.updateDataByPlant(dateTime, company);
		}else{
			apiService.updateIndicatorData(dateTime);
		}
		
		return ResponseEntity.noContent().build();
	}
	
	@GetMapping("/findAllLog")
	@RequiresPermissions("yddemo:indicatorDataExtractionLog:manage")
	@ApiOperation(value = "查询全部日志", notes = "查询全部日志信息")
	ResponseEntity<Page<IndicatorMapperDataLog>> findOne(@CustomPagingParam PagingParam pagingParam) {
		List<SearchFilter> searchFilters = new ArrayList<>();
		Specification<IndicatorMapperDataLog> spec = buildFilterRequest(searchFilters);
		PageRequest pageRequest = buildPageRequest(pagingParam);
		List<IndicatorMapperDataLog> findAll = logService.findAll(spec);
		long total = 0;
		if (pageRequest != null) {
			total = findAll.size();
		}
		Page<IndicatorMapperDataLog> page = new PageImpl<IndicatorMapperDataLog>(findAll, pageRequest, total);
		return ResponseEntity.ok(page);
	}

}
